<template>
  <el-dialog :model-value="dialogData.isShow" width="700px" :title="dialogData.id?'编辑':'新增'"
             @close="closeDialog" draggable>
    <el-form :model="form" ref="formRef" :rules="rules" label-width="100px">
      <el-row :gutter="10">
        <el-col :span="20">
          <el-form-item label="专题名称" prop="title">
            <el-input v-model="form.title" maxlength="255" placeholder="请输入专题名称"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item label="图片" prop="imgUrl" style="height: 200px">
            <Upload v-model:value="form.imgUrl" type="image"
                    :customStyle="{'width':'120px','height':'120px'}"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-select
                v-model="form.status"
                placeholder="请选择状态"
                style="width: 240px"
            >
              <el-option
                  v-for="item in [{'id': '1', 'title': '展示'}, {'id': '0', 'title': '不展示'}]"
                  :key="item.id"
                  :label="item.title"
                  :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="20">
          <el-form-item label="排序" prop="sort">
            <el-input v-model="form.sort" maxlength="255" placeholder="请输入排序"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import {ElMessage, FormInstance, FormRules} from "element-plus";
import {addArticleTopics, getArticleTopics, updateArticleTopics} from "@/api/app/articleTopics";

const formRef = ref<FormInstance>()

const emits = defineEmits<{
  (event: 'refresh'): void
}>()

// 表单
let form: any = ref({
  id: null,
  title: null,
  imgUrl: null,
  status: '1',
  sort: null,
});

// 效验规则
const rules = reactive<FormRules>({
  title: [
    {
      required: true,
      message: '请输入标题',
      trigger: 'blur',
    },
  ],
  imgUrl: [
    {
      required: true,
      message: '请上传',
      trigger: 'blur',
    },
  ],
  status: [
    {
      required: true,
      message: '请选择',
      trigger: 'blur',
    }
  ]
})

// 获取详情
const getDetails = (id: string) => {
  getArticleTopics(id).then(res => {
    form.value = Object.assign({}, form.value, res);
  })
  console.log(form)
}

// 弹框数据
const dialogData = reactive({
  isShow: false,
  id: null
})

// 打开弹框
const openDialog = async (id: string) => {
  dialogData.isShow = true;
  dialogData.title = '新增专题';
  if (id) {
    dialogData.id = id;
    dialogData.title = '编辑专题';
    getDetails(id);
  }
}

// 关闭弹框
const closeDialog = () => {
  dialogData.isShow = false;
  dialogData.id = null;
  form.value = {};
}
// 提交

const submit = async () => {
  if (!formRef.value) return;
  await formRef.value.validate((valid: any) => {
    if (valid) {
      let data = form.value;
      if (data.id) {
        updateArticleTopics({...data}).then(() => {
          ElMessage.success('操作成功');
          closeDialog();
          emits('refresh');
        })
      } else {
        addArticleTopics({...data}).then(() => {
          ElMessage.success('操作成功');
          closeDialog();
          emits('refresh');
        })
      }
    }
  })
}


defineExpose({
  openDialog,
});
</script>

<style scoped>


</style>
